<template>
  <view class="content">
    <u--image
      width="448rpx"
      height="180rpx"
      mode="aspectFit"
      src="/static/logo.png"
    ></u--image>
    <button
      class="loginBtn"
      open-type="getPhoneNumber"
      @getphonenumber="decryptPhoneNumber"
    >
      微信授权登录
    </button>
	<!-- <button
	  class="loginBtn"
	  @click="wxLogin"
	>
	  微信授权登录
	</button> -->
	
  </view>
</template>

<script>
import { getOpenIdByCode, getPhoneNumber, userInfo,wxLogin } from "@/api";
import { setToken } from "@/utils/auth";
export default {
  data() {
    return {
      session_key: "",
      openid: "",
    };
  },
  onShow() {
    let _this = this;
    uni.login({
      provider: "weixin", //使用微信登录
      success: function (loginRes) {
		  console.log('code',loginRes);
        getOpenIdByCode({ code: loginRes.code }).then((res) => {
          _this.openid = res.data.openid;
          _this.session_key = res.data.session_key;
        });
      },
    });
  },
  methods: {
    decryptPhoneNumber(data) {
      if (data.detail.iv) {
        getPhoneNumber({
          encryptedData: data.detail.encryptedData,
          iv: data.detail.iv,
          openId: this.openid,
          session_key: this.session_key,
        }).then((res) => {
			console.log(res);
          setToken(res.token);
          // this.$route({
          //   type: "back",
          // });
		  uni.reLaunch({
		  	url: '/pages/index/index'
		  })
        });
      }
    },
	wxLogin(){
		wxLogin().then((res) => {
			setToken(res.token);
			// this.$route({
			//   type: "back",
			// });
			uni.reLaunch({
				url: '/pages/index/index'
			})
		})
	}
  },
};
</script>

<style scoped>
.loginBtn {
  width: 610rpx;
  height: 96rpx;
  background: linear-gradient(83deg, #48b453 0%, #2c9f79 100%);
  border-radius: 49rpx;
  font-size: 32rpx;
  line-height: 96rpx;
  margin-top: 134rpx;
  box-sizing: border-box;
  color: #ffffff;
}

.content {
  height: calc(50vh + 48rpx);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.logo {
  width: 208rpx;
  margin-bottom: 134rpx;
  z-index: 99;
}
</style>
